﻿// Copyright (c) Yoganand R. All rights reserved.
// For continued development and updates, visit http://yogsbreath.com
using System;
using System.Collections.Generic;
using System.Web.UI;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// Used by WriteScript to communicate how the object
    /// definition should be output.
    /// </summary>
    public enum ScriptType
    {
        StandAlone,
        NewOnly,
        ConfigOnly
    }

    /// <summary>
    /// This is an implementation of ScriptControlDescriptor which gives us
    /// access to the properties which are added to the base collection, which
    /// is declared as private by the .NET framework
    /// </summary>
    public class ScriptControlDescriptorBase : ScriptControlDescriptor
    {
        //this is a copy of the registered properties list so we can use it
        protected SortedList<String, Object> _properties = new SortedList<String, Object>();

        protected List<String> _prototype = new List<String>();
        protected List<String> _script = new List<String>();

        public ScriptControlDescriptorBase(String type, String elementId) : base (type, elementId)
        {
            
        }

        /// <summary>
        /// Builds the client script used to define the object in the browser
        /// </summary>
        /// <remarks>
        /// Because the framework function GetScript() is protected internal only 
        /// ScriptManager can call it, but in order for the Abstraction framework
        /// to get the output WriteScript() wraps GetScript() and exposes it
        /// publicly.
        /// </remarks>
        /// <returns></returns>
        public virtual String WriteScript()
        {
            return base.GetScript();
        }

        /// <summary>
        /// Builds the client script used to define the object in the browser
        /// </summary>
        /// <remarks>
        /// isStandAlone will be ignored if no concrete implementation is provided
        /// </remarks>
        /// <param name="isStandAlone">includes var declaration and terminates definition (;)</param>
        /// <returns></returns>
        public virtual String WriteScript(ScriptType scriptType)
        {
            // parameter is ignored unless implementation is provided
            return WriteScript();
        }

        /// <summary>
        /// Defers the script building to the overridden WriteScript method
        /// so child classes may build their own scripts
        /// </summary>
        /// <returns></returns>
        protected override string GetScript()
        {
            return WriteScript(ScriptType.StandAlone);
        }

        public virtual string AppendScript()
        {
            return "";
        }

        /// <summary>
        /// Adds property and a value to the descriptors for this control
        /// </summary>
        /// <remarks>
        /// This is a bit of a hack, but for now it will allow 
        /// us to create a copy of the descriptors for our own use.
        /// </remarks>
        public void AddPropertyBase(String name, Object value)
        {
            if (String.IsNullOrEmpty(name))
                throw new ArgumentNullException("name");

            _properties[name] = value;

            base.AddProperty(name, value);
        }

        public void AddScriptBase(String value)
        {
            if (String.IsNullOrEmpty(value))
                return;

            _script.Add(value);            
        }

        /// <summary>
        /// Adds property and a value to the descriptors for this control
        /// </summary>
        /// <remarks>
        /// This is a bit of a hack, but for now it will allow 
        /// us to create a copy of the descriptors for our own use.
        /// </remarks>
        public void AddPrototype(String value)
        {
            if (String.IsNullOrEmpty(value))
                return;

            _prototype.Add(value);
        }

        public new void AddProperty(String name, Object value)
        {
            this.AddPropertyBase(name, value);
        }

        public bool Overloaded { get; set; }

    }
}
